extends VBoxContainer

@onready var _projectDropDown:OptionButton = $"Project/OptionButton"

@onready var _aInput = $"A"
@onready var _bInput = $"B"
@onready var _cInput = $"C"
@onready var _eDropDown:OptionButton = $"E/OptionButton"

@onready var _oldVersionText = $"CurrentVersion/VersionNumber"
@onready var _newVersionText =  $"FinalVersion/VersionNumber"

@onready var _contentInput = $"VersionChangeInput"

var hasInit:bool = false

func _ready() -> void:
	_refresh_project_drop_down()
	
	_refresh_old_version_content()
	_refresh_new_version_text()
	
	hasInit = true

	
func _refresh_project_drop_down():
	_projectDropDown.clear()
	
	var defaultName = Global.default_project_name
	var configs = Global.config_datas
	
	var temp = 0
	for key in configs.keys():
		_projectDropDown.add_item(key)
		if key == defaultName:
			_projectDropDown.select(temp)
		temp = temp + 1


func _on_a_on_value_changed(value: Variant) -> void:
	var defaultName = Global.default_project_name
	var config = Global.config_datas[defaultName]
	if value > config.a:
		_bInput.change_value(0)
		_cInput.change_value(0)
	config.a = value
	_refresh_new_version_text()


func _on_b_on_value_changed(value: Variant) -> void:
	var defaultName = Global.default_project_name
	var config = Global.config_datas[defaultName]
	if value > config.b:
		_cInput.change_value(0)
	config.b = value
	_refresh_new_version_text()


func _on_c_on_value_changed(value: Variant) -> void:
	var defaultName = Global.default_project_name
	var config = Global.config_datas[defaultName]
	config.c = value
	_refresh_new_version_text()


func _on_e_drop_down_item_selected(index: int) -> void:
	var defaultName = Global.default_project_name
	var config = Global.config_datas[defaultName]
	config.e = index
	_refresh_new_version_text()
	

func _refresh_new_version_text():
	var selectIndex = _projectDropDown.get_selected_id()
	var selectProjectName = _projectDropDown.get_item_text(selectIndex)
	var config = Global.config_datas[selectProjectName]
	_newVersionText.text = config.get_version_str(true)
	
	
func _refresh_old_version_content():
	var selectIndex = _projectDropDown.get_selected_id()
	var selectProjectName = _projectDropDown.get_item_text(selectIndex)
	var config = Global.config_datas.get(selectProjectName)
	_oldVersionText.text = config.get_version_str(false)
	
	_aInput.init_value(config.a)
	_bInput.init_value(config.b)
	_cInput.init_value(config.c)
	_eDropDown.select(config.e)
	
	_contentInput.text = ""
	var fileName = selectProjectName + "_" + _oldVersionText.text + ".txt"
	var oldVersionPath = config.save_path + "/" + fileName
	print(oldVersionPath)
	var fa = FileAccess.open(oldVersionPath, FileAccess.READ)
	if fa != null:
		_contentInput.text = fa.get_as_text()


func _on_project_drop_down_item_selected(index: int) -> void:
	var selectedProjectName = _projectDropDown.get_item_text(index)
	Global.save_default_select_project_name(selectedProjectName)
	_refresh_old_version_content()
	_refresh_new_version_text()


func _on_clear_btn_button_down() -> void:
	_contentInput.text = ""


func _on_create_button_button_down() -> void:
	var selectIndex = _projectDropDown.get_selected_id()
	var selectProjectName = _projectDropDown.get_item_text(selectIndex)
	var config = Global.config_datas[selectProjectName]
	
	var fileName = config.project_name + "_" + _newVersionText.text
	
	var savePath = config.save_path + "/" + fileName + ".txt"
	var fa = FileAccess.open(savePath, FileAccess.WRITE)
	fa.store_string(_contentInput.text)
	config.save_config()
	
	OS.shell_open(config.save_path)


func _on_visibility_changed() -> void:
	if visible and hasInit:
		_refresh_project_drop_down()
